/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.glc.hms.core.common.log;

import com.glc.hms.holder.SpringContextHolder;
import com.glc.hms.system.dao.SysLogMapper;
import com.glc.hms.system.entity.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.DependsOn;

import java.util.TimerTask;

/**
 * 日志操作任务创建工厂
 *
 * @author Santa
 * @since 2019/11/14
 */
@Slf4j
@DependsOn("SpringContextHolder")
public class LogTaskFactory {


    private static SysLogMapper sysLogMapper = SpringContextHolder.getBean(SysLogMapper.class);

    public static TimerTask loginLog(final String userName,final String operationName,final String param,final String methodName,final Long time,final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    SysLog loginLog = LogFactory.createLoginLog(userName,operationName,param,methodName,time,ip);
                    sysLogMapper.insert(loginLog);
                } catch (Exception e) {
                    log.error("创建登录日志异常!", e);
                }
            }
        };
    }

    public static TimerTask buinessLog(final String userName,final String operationName,final String param,final String methodName,final Long time,final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                SysLog loginLog = LogFactory.createOperationLog(userName,operationName,param,methodName,time,ip);
                try {
                    sysLogMapper.insert(loginLog);
                } catch (Exception e) {
                    log.error("创建登录失败异常!", e);
                }
            }
        };
    }

}
